package com.example.productdataserviceprovider.service.library;

import cn.hutool.http.HtmlUtil;
import com.example.productdataserviceprovider.service.AbstractService;
import com.example.productdataserviceprovider.service.library.model.SearchType;
import com.example.productdataserviceprovider.service.search.model.SearchResultModel;
import com.example.productdataserviceprovider.service.utils.HttpUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.io.*;
import java.nio.charset.StandardCharsets;
import java.util.*;
import java.util.concurrent.CountDownLatch;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

@Deprecated
@Service
public class SearchCityServiceImpl extends AbstractService implements SearchCityService{
    private  Pattern titlePattern=Pattern.compile("<h3 class=\"t\">(.*?)</h3>");
    @Override
    public Map<String, String> getCitysFromFile(String file) {
        Map<String,String> librarys=new HashMap<>();
        BufferedReader reader=null;
        Pattern pattern1=Pattern.compile("(.*?)(省|市|区|州|县|旗|团|盟|综合)");
        try {
            reader=new BufferedReader(new InputStreamReader(new FileInputStream(file), StandardCharsets.UTF_8));
            String line=null;
            int count=0;
            while((line=reader.readLine())!=null){
                if(!StringUtils.hasText(line))continue;
                count++;
                Matcher matcher=pattern1.matcher(line);
                if(matcher.find()){
                    String city=matcher.group(1);
                    if(StringUtils.hasText(city)){
                        librarys.put(city,"");
                    }
                    else{
                        logger.info("city is empty="+city+",line="+line);
                    }

                }else{
                    librarys.put(line,"");
                }
            }
            logger.info("count="+count);
        } catch (Exception e) {
            e.printStackTrace();
        }
        logger.info("totalSize="+librarys.size());
        return librarys;
    }
    private boolean checkCityExist(String title,String k1,String k2,Map<String,String> citys,SearchType type){
        boolean flag=false;
        for(Map.Entry<String,String> city:citys.entrySet()){
            if(!StringUtils.hasText(city.getKey())||city.getKey().length()==1)continue;
            switch(type){
                case MH:
                    if(title.indexOf(city.getKey())!=-1){
                        return true;
                    }
                    break;
                case JQ:
//                    logger.info("title=={},city={}.",title,city.getKey());
                    if(title.indexOf(k1) != -1 && title.indexOf(k2) != -1 && title.indexOf(city.getKey()) != -1){
                        return true;
                    }
            }

        }
        return flag;
    }
    public void writeToFile(List<SearchResultModel> results,String destFile){
        File file=new File(destFile);
        BufferedWriter writer =null;
        try {
            writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(destFile), StandardCharsets.UTF_8));
            for (SearchResultModel data : results) {
                writer.write(data.toString());
                writer.newLine();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }finally{
            if(writer!=null){
                try {
                    writer.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }


    }


    private List<SearchResultModel> analyseOne(String k1,String k2,Map<String,String> citys,SearchType type){
        List<SearchResultModel> datas=new ArrayList<SearchResultModel>();
        List<SearchResultModel> resultList=new ArrayList<>();
        int perPageSize=10;
        int totalPage=30;//总共查询20页// TODO
        int total=citys.size();
        for(int page=0;page<totalPage;page++){

            int pn=page*perPageSize;
            String baiduUrl="https://www.baidu.com/s?wd="+k1+"%20"+k2+"&pn="+pn;
            String result="";
            try {
                result= HttpUtils.doGet(baiduUrl).replaceAll("(\r\n|\r|\n|\n\r)","");
//                logger.info("result="+result);
                Matcher matcher=titlePattern.matcher(result);
                while(matcher.find()){
                    String titleHtml=matcher.group(1);
                    Document document = Jsoup.parse(titleHtml);
//                    logger.info("document.toString={}",document.toString());
                    Element element=document.getElementsByTag("a").first();
                    String title= HtmlUtil.cleanHtmlTag(titleHtml).trim();
                    String url=element.attr("href");

                    boolean flag=checkCityExist(title,k1,k2,citys,type);
//                    logger.info("title:{},k1={},k2={},flag={}",title,k1,k2,flag);
                    if(flag){
                        SearchResultModel model=new SearchResultModel();
                        model.setK1(k1);
                        model.setK2(k2);
                        model.setPage(pn);
                        model.setUrl(baiduUrl);
                        model.setTitle(title);
                        model.setUrl(url);
                        datas.add(model);
                    }
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
            if(!StringUtils.hasText(result)){
                logger.info("搜索结果为空");
                continue;
            }

        }
       return datas;
    }
    @Override
    public List<SearchResultModel> analyse(String keys, String keys2, Map<String, String> citys, CountDownLatch latch,SearchType type) {
        List<SearchResultModel> results=new ArrayList<>();
        String[] keys2Array=keys2.split(",");
        for(String k2:keys2Array){
            List<SearchResultModel> datas=analyseOne(keys,k2,citys,type);
            logger.info("k1={},k2={},size={}",keys,k2,datas.size());
            results.addAll(datas);
        }

        latch.countDown();
        return results;
    }

    @Override
    public void writeCityToFile(Map<String,String> citys, String destFile) {
        File file=new File(destFile);
        BufferedWriter writer =null;
        try {
            writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(destFile), StandardCharsets.UTF_8));
            for (Map.Entry<String,String> data : citys.entrySet()) {
                if(!StringUtils.hasText(data.getKey())){
                    logger.info("hasNoText={}",data.getKey());
                }
                writer.write(data.getKey());
                writer.newLine();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }finally{
            if(writer!=null){
                try {
                    writer.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }


    public static void main(String[] args) {
        SearchCityService service=new SearchCityServiceImpl();
        long start=System.currentTimeMillis();
        String file="D:\\library\\library3\\libraryDatas.txt";
        Map<String, String> citys=service.getCitysFromFile(file);
        System.out.println(citys.size());
        service.writeCityToFile(citys,"D:/library/library3/simpleCitys.txt");
//        String key1="免费开放工作实施方案,免费开放工作方案,免费开放实施方案,免费开放方案,免费开放具体实施方案,免费开放管理制度,免费开放管理办法,免费开放服务指南,免费开放制度";
        String key1="公共文化场馆";
        String key2="免费开放实施办法";
        SearchType type=SearchType.JQ;
        String[] keys=key1.split(",");
        CountDownLatch latch=new CountDownLatch(keys.length);
//        List<SearchResultModel> datas=new ArrayList<>();
        for(String key:keys){
            new Thread(()->{

                String destFile="D:/library/library3/reults-{key}-{searchtype}.txt";
                List<SearchResultModel> results= service.analyse(key,key2,citys,latch,type);
                logger.info("key={},total.ize={}",key,results.size());
                destFile=destFile.replaceAll("\\{key\\}",key).replaceAll("\\{searchtype\\}",type.getValue());
                service.writeToFile(results,destFile);
//                datas.addAll(results);
            }).start();
        }

        try {
            latch.await();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
//        logger.info("totalResult:{}"+datas.size());
        long end=System.currentTimeMillis();
        System.out.println("总共花费时间:"+(end-start)/100);


    }
}
